package com.AAL.Action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.AAL.DAO.TypeOfAutoDAO;
import com.AAL.DAO.TypesOfWheelDAO;
import com.AAL.DAO.WheelDetailDAO;
import com.AAL.Model.TypeOfAuto;
import com.AAL.Model.TypesOfWheel;
import com.AAL.Model.WheelDetail;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class WheelForDealerAction extends ActionSupport implements SessionAware {

	private Map<String, Object> session;
	private TypeOfAutoDAO lnkTypeOfAutoDAO;
	private TypesOfWheelDAO lnkTypesOfWheelDAO;
	private WheelDetailDAO lnkWheelDetailDAO;

	private List<TypeOfAuto> lstTypeOfAutos;
	private List<TypesOfWheel> lstTypesOfWheels;
	private List<WheelDetail> lstWheelDetails;
	private WheelDetail wheelDetail;
	private TypeOfAuto typeOfAuto;
	private TypesOfWheel typesOfWheel;

	/*
	 * Cac bien can cho viec phan trang
	 */
	private int prePage;
	private int nextPage;
	private int totalPageOfPayment;
	private int currentPage;
	private int startPage;
	private int numRecordInPage = 10;

	public WheelForDealerAction() {
		this.lnkTypeOfAutoDAO = new TypeOfAutoDAO();
		this.lnkWheelDetailDAO = new WheelDetailDAO();
		this.lnkTypesOfWheelDAO = new TypesOfWheelDAO();
	}

	public String getLstTypeOfWheelByTypeOfAutos() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		try {
			this.typeOfAuto = lnkTypeOfAutoDAO.getTypeOfAuto(request
					.getParameter("toaid"));

			this.lstTypesOfWheels = this.lnkTypesOfWheelDAO
					.getLstTypesOfWheelsByTypeOfAutos(this.typeOfAuto);
			if (this.lstTypesOfWheels.size() == 0) {
				this.lstTypesOfWheels = null;
			}

		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String getLstWheelDetailsByTypeOfWheel() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		try {
			this.typesOfWheel = lnkTypesOfWheelDAO.getTypesOfWheel(request
					.getParameter("towid"));
			/*
			 * Xoa cac thong lin luu tru cu
			 */
			session.remove("viewLstWheelDetailByTypeOfWheel");
			session.remove("totalPageOfviewLstWheelDetail");
			session.remove("startPage");
			session.remove("typesOfWheel");
			/*
			 * Cap nhat moi
			 */
			List<WheelDetail> lstWheelDetailsByTypeOfAutos = this.lnkWheelDetailDAO
					.getLstWheelDetailsByTypeOfWheel(this.typesOfWheel);
			session.put("typesOfWheel", this.typesOfWheel);
			session.put("viewLstWheelDetailByTypeOfWheel",
					lstWheelDetailsByTypeOfAutos);
			// lay so bang ghi de phan trang
			int totalRecordOfviewLstWheelDetail = lstWheelDetailsByTypeOfAutos
					.size();
			// hien thi 5 bang ghi tren moi trang
			this.totalPageOfPayment = 0;

			if (totalRecordOfviewLstWheelDetail % numRecordInPage == 0) {
				this.totalPageOfPayment = totalRecordOfviewLstWheelDetail
						/ numRecordInPage;
			} else {
				this.totalPageOfPayment = totalRecordOfviewLstWheelDetail
						/ numRecordInPage + 1;
			}

			session.put("totalPageOfviewLstWheelDetail",
					this.totalPageOfPayment);
			this.lstWheelDetails = pagingWheelDetails(
					lstWheelDetailsByTypeOfAutos, 1);
			if (this.lstWheelDetails.size() == 0)
				this.lstWheelDetails = null;
			paging(0, 0);

		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String paggingViewLstWheelDetailsByTypeOfWheel() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		try {
			if (session.get("viewLstWheelDetailByTypeOfWheel") != null) {
				List<WheelDetail> lstWheelDetailsByTypeOfAutos = (List<WheelDetail>) session
						.get("viewLstWheelDetailByTypeOfWheel");
				// lay so bang ghi de phan trang
				int page = Integer.parseInt(request.getParameter("p"));
				this.lstWheelDetails = pagingWheelDetails(
						lstWheelDetailsByTypeOfAutos, page);
				if (this.lstWheelDetails.size() == 0)
					this.lstWheelDetails = null;
				/* Phan trang */
				int totalPage = (Integer) session
						.get("totalPageOfviewLstWheelDetail");
				paging(page, totalPage);
				this.typesOfWheel = (TypesOfWheel) session.get("typesOfWheel");
			}
		} catch (Exception e) {
			addActionError("Loi:" + e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}

	public String getWheelDetailByID() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
		.getContext().get(ServletActionContext.HTTP_REQUEST);
		try {
			System.out.println("Chi tiet lop ");
			this.wheelDetail = lnkWheelDetailDAO.getWheelDetail(request.getParameter("w"));
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	/*
	 * Minh
	 */
	private List<WheelDetail> pagingWheelDetails(
			List<WheelDetail> lstAllWheelDetails, int page) {
		List<WheelDetail> lstTemp = new ArrayList<WheelDetail>();
		int maxlength = lstAllWheelDetails.size();

		for (int i = 0; i < numRecordInPage; i++) {
			int temp = numRecordInPage * (page - 1) + i;
			if (temp < maxlength) {
				lstTemp.add(lstAllWheelDetails.get(temp));
			} else {
				break;
			}
		}
		return lstTemp;
	}

	private void paging(int page, int totalPage) {

		if (page == 0) {
			this.startPage = 1;
			this.prePage = -1;
			this.nextPage = 2; // set 2 luon cung dc, vi voi 1 page thi no
								// se ko hien thi phan trang ra.
			this.currentPage = 1;
			session.put("startPage", this.startPage);
		} else {
			this.startPage = (Integer) session.get("startPage");
			this.totalPageOfPayment = totalPage;
			this.currentPage = page;
			this.prePage = page - 1;
			if (this.prePage <= 0)
				this.prePage = -1;
			this.nextPage = page + 1;
			if (this.nextPage > totalPageOfPayment) {
				this.nextPage = -1;
			}

			if (this.currentPage - this.startPage == 4) {
				this.startPage += 2;

				if (this.startPage + 5 >= this.totalPageOfPayment) {
					this.startPage = this.totalPageOfPayment + 1 - 5;
				}
			} else if (this.currentPage - this.startPage == 0) {

				this.startPage -= 2;
				if (this.startPage <= 0) {
					this.startPage = 1;
				}
			}
			session.put("startPage", this.startPage);
		}
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public List<TypeOfAuto> getLstTypeOfAutos() {
		return lstTypeOfAutos;
	}

	public void setLstTypeOfAutos(List<TypeOfAuto> lstTypeOfAutos) {
		this.lstTypeOfAutos = lstTypeOfAutos;
	}

	public List<TypesOfWheel> getLstTypesOfWheels() {
		return lstTypesOfWheels;
	}

	public void setLstTypesOfWheels(List<TypesOfWheel> lstTypesOfWheels) {
		this.lstTypesOfWheels = lstTypesOfWheels;
	}

	public List<WheelDetail> getLstWheelDetails() {
		return lstWheelDetails;
	}

	public void setLstWheelDetails(List<WheelDetail> lstWheelDetails) {
		this.lstWheelDetails = lstWheelDetails;
	}

	public void setWheelDetail(WheelDetail wheelDetail) {
		this.wheelDetail = wheelDetail;
	}

	public WheelDetail getWheelDetail() {
		return wheelDetail;
	}

	public TypeOfAuto getTypeOfAuto() {
		return typeOfAuto;
	}

	public void setTypeOfAuto(TypeOfAuto typeOfAuto) {
		this.typeOfAuto = typeOfAuto;
	}

	public TypesOfWheel getTypesOfWheel() {
		return typesOfWheel;
	}

	public void setTypesOfWheel(TypesOfWheel typesOfWheel) {
		this.typesOfWheel = typesOfWheel;
	}

	public int getPrePage() {
		return prePage;
	}

	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public int getTotalPageOfPayment() {
		return totalPageOfPayment;
	}

	public void setTotalPageOfPayment(int totalPageOfPayment) {
		this.totalPageOfPayment = totalPageOfPayment;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getStartPage() {
		return startPage;
	}

	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}

}
